/**
 * \file XMLElement.cpp
 * \brief XMLElement definition class
 * \author Edouard ROGE
 * \date 16 October 2012
 */

#include "XMLElement.h"

namespace Core
{
	namespace File
	{
		XMLElement::XMLElement()
			: XMLNode()
		{

		}

		XMLElement::~XMLElement()
		{
			for (uint32 i = 0; i < m_Attributes.size(); ++i)
				delete m_Attributes[i];
		}

		void XMLElement::SetAttributes(const std::vector<XMLAttribute*> & _attributes)
		{
			m_Attributes = _attributes;
		}

		const std::vector<XMLAttribute*> &  XMLElement::GetAttributes() const
		{
			return m_Attributes;
		}

		error XMLElement::Save(File & _file, uint32 _iDepth) const
		{
			//indentation
			for (uint32 i = 0; i < _iDepth; ++i)
				_file << "\t";

			// name
			_file << "<" << GetName();

			// attributes
			for (uint32 i = 0; i < m_Attributes.size(); ++i)
				_file << " " << m_Attributes[i]->GetName() << "=\"" << m_Attributes[i]->GetStringValue() << "\"";

			// children if possible
			if (m_Children.size() == 0)
			{
				_file << "/>\n";
			}
			else
			{
				_file << ">\n";
				for (uint32 i = 0; i < m_Children.size(); ++i)
					m_Children[i]->Save(_file, _iDepth + 1);

				// end balise
				_file << "</" << GetName() << ">\n";
			}

			return errOK;
		}
	}
}
